package com.speedata.uhf;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.speedata.libuhf.IUHFService;

import java.util.Objects;

/**
 * 搜索灯光寻卡
 * Created by 徐岩帅 on 20230608
 * fix by xuyan  优化可以读其他区
 *
 * @author My_PC
 */
public class SearchLightActivity extends BaseActivity implements View.OnClickListener {


    private IUHFService iuhfService;

    private String epcToStr;
    private String strAddr;
    private String strCount;
    private String strPasswd;

    private String mWhichChoose;
    private ImageView imageViewSearch;
    Handler handler = new Handler();

    private SoundPool soundPool;
    private int soundId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_light);
        getBundle();
        initView();
        initData();
    }

    public void initView() {
        //返回
        ImageView mIvQuit = findViewById(R.id.search_title_iv);
        //停止
        Button mStopBtn = findViewById(R.id.search_stop);
        mStopBtn.setOnClickListener(this);
        mIvQuit.setOnClickListener(this);
        imageViewSearch = findViewById(R.id.search_card);
    }


    public void initData() {


        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this).load(R.drawable.bg_search_card).apply(options).into(imageViewSearch);
        iuhfService = MyApp.getInstance().getIuhfService();

        initSoundPool();
    }

    public void initSoundPool() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                    .build();
            soundPool = new SoundPool.Builder()
                    .setMaxStreams(1)
                    .setAudioAttributes(attributes)
                    .build();
        } else {
            soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        }
        soundId = soundPool.load(this, R.raw.beep, 0);
    }

    public void getBundle() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        epcToStr = Objects.requireNonNull(bundle).getString("epcNumber");
        strAddr = Objects.requireNonNull(bundle).getString("strAddr");
        strCount = Objects.requireNonNull(bundle).getString("strCount");
        strPasswd = Objects.requireNonNull(bundle).getString("strPasswd");
        mWhichChoose = Objects.requireNonNull(bundle).getString("whichChoose");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //卡早已选好，开始循环读卡
        handler.post(runnable);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            /*
             *  浮光掠影
             */
            try {
                final int addr = Integer.parseInt(strAddr);
                final int count = Integer.parseInt(strCount);
                final int whichChoose = Integer.parseInt(mWhichChoose);
                soundPool.play(soundId, 1, 1, 0, 0, 1);
                new Thread(() -> {
                    iuhfService.readArea(whichChoose, addr, count, strPasswd);
                    Log.w("read", "mWhichChoose = " + whichChoose
                            + "\n addr = " + addr
                            + "\n addr = " + count
                            + "\n addr = " + strPasswd);
                }
                ).start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            handler.postDelayed(runnable, 1000);
        }
    };


    //新的Listener回调参考代码
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_title_iv:
            case R.id.search_stop:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStop() {
        // 停止循环读卡
        handler.removeCallbacks(runnable);
        soundPool.release();
        Log.w("stop", "im stopping");
        super.onStop();
    }
}
